package com.shiguiwu.springboot3.config;

import cn.hutool.extra.spring.EnableSpringUtil;
import com.shiguiwu.springboot3.interceptor.SqlAdapterInterceptor;
import com.shiguiwu.springboot3.properties.MybatisProperties;
import org.apache.ibatis.mapping.DatabaseIdProvider;
import org.apache.ibatis.mapping.VendorDatabaseIdProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.sql.DataSource;
import java.util.Properties;

/**
 * @description:
 * @author: stone
 * @date: Created by 2023/7/19 15:19
 * @version: 1.0.0
 * @pakeage: com.shiguiwu.springboot3.config
 */
@Configuration
@EnableSpringUtil
@EnableConfigurationProperties(MybatisProperties.class)
public class MybatisConfig {

    @Bean
    public DatabaseIdProvider getDatabaseIdProvider() {
        VendorDatabaseIdProvider databaseIdProvider = new VendorDatabaseIdProvider();
        Properties properties = new Properties();
        properties.setProperty("MySQL", "mysql");
        properties.setProperty("DM DBMS", "dm");
        databaseIdProvider.setProperties(properties);
        return databaseIdProvider;
    }

    @ConditionalOnProperty(prefix = "mybatis.adapter",name = "enable", havingValue = "true", matchIfMissing = true)
    @Bean
    public SqlAdapterInterceptor sqlAdapterInterceptor(DataSource dataSource, MybatisProperties properties) {
        return new SqlAdapterInterceptor(getDatabaseIdProvider(), dataSource, properties);
    }

}
